/**
 * 返回普通字符串
 * @param ctx
 * @param next
 * @returns {Promise<void>}
 */
var api_request = async (ctx, next) => {

    // ctx.set('__cess_media_type__', 'cess2');
    // ctx.set('content-type','application/json;charset=utf-8')
    //ctx.status = 404;
    ctx.response.body = "ok";
}

/**
 * 返回 json 字符串
 * @param ctx
 * @param next
 * @returns {Promise<void>}
 */

var api_json_request = (data)=>{
   return async (ctx, next) => {
       ctx.set('content-type','application/json;charset=utf-8')
       ctx.response.body = JSON.stringify(data);
   }
}

var api_json_str_request = api_json_request("ok")

var api_json_obj_request = api_json_request({
    name: 'name1',
    value: 'value1'
});

var api_json_number_request = api_json_request(10);
var api_json_str_array_request = api_json_request(['one','two','thee']);
var api_json_obj_array_request = api_json_request([{name:'one'},{name:'two'},{name:'thee'}]);


/**
 * 返回 json 字符串
 * @param ctx
 * @param next
 * @returns {Promise<void>}
 */

var api_json_exception = async (ctx, next) => {
    ctx.set('__cess_media_type__', 'cess2');
    ctx.set('__http_comm_with_error__', '');
    ctx.set('content-type','application/json;charset=utf-8')
    ctx.response.body = JSON.stringify({
        code: 3,
        message: 'json excepton',
    });
}

var http_error = (error, errorMessage)=>{
    return async (ctx, next) => {
        ctx.status = error;
        ctx.response.body = errorMessage;
    }
}
/**
 * 导出访问的地址，及返回的数据
 * @type {{"/api/test": Promise<void>}}
 */
module.exports = {
    '/api/request': api_request,
    '/api/json/str': api_json_str_request,
    '/api/json/obj': api_json_obj_request,
    '/api/json/number': api_json_number_request,
    '/api/json/str_array': api_json_str_array_request,
    '/api/json/obj_array': api_json_obj_array_request,
    '/api/json/exception': api_json_exception,
    '/http/error/400': http_error(400,'request error'),
    '/http/error/500': http_error(500,'internal error'),
}
